package com.erju.controller;

import com.erju.pojo.User;
import com.erju.service.LoginService;
import com.erju.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;


/**
 * @Description: 登陆接口
 * @author: 86132
 * @date: 2022.03.04
 */
@RestController
public class LoginController {

    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    LoginService loginService;


    /**
     * 用户名登陆
     *
     * @param user
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(path = "/loginByUsername")
    public Result<?> loginByUsername(@RequestBody User user) throws UnsupportedEncodingException {

        log.info(user.toString());
        //服务模块处理
        Result<?> result = loginService.userNameLogin(user);
        return result;
    }

    /**
     * 没有权限跳转到这里
     *
     * @return
     */
    @RequestMapping("/unauthorized")
    public Result unauthorized() {
        return Result.error("没有权限");
    }

    @RequestMapping("/loginOut")
    public Result loginOut(ServletRequest request, ServletResponse response) {
        return loginService.loginOut(request, response);
    }
    /**
     * 人脸登录
     * @return
     * @throws Exception
     */
    @RequestMapping("/face-login")
    public  Result searchface(@RequestBody String imagebast64) throws Exception {

        StringBuffer stringBuffer = new StringBuffer(imagebast64);
        log.info(imagebast64);
        Result userId = loginService.loginByFace(stringBuffer);
//
//        request.getSession().setAttribute("userId",userId);
//        request.getSession().setAttribute("username","派大星");
        return null;
    }
}
